﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SchoolLibrary.Entity;
using SchoolLibrary.DataBase.DAO;

namespace SchoolLibrary.DataBase.Builder
{
    class RegistryJournalBuilder : AbstractEntityBuilder
    {
        public override Identity Build(DataRow row)
        {
            RegistryJournal journal = new RegistryJournal();
            BuildEntity(journal, row);
            return journal;
        }

        protected override void BuildEntity(Identity entity, DataRow row)
        {
            base.BuildEntity(entity, row);
            RegistryJournal journal = (RegistryJournal)entity;
            int bookId = Int32.Parse(row[RegistryJournal.BOOK_ID_COLUMN_NAME].ToString());
            journal.Book = BookDAO.getInstance().findById(bookId);


            int pupilId = Int32.Parse(row[RegistryJournal.PUPIL_ID_COLUMN_NAME].ToString());
            journal.Pupil = PupilDAO.getInstance().findById(pupilId);
            
            
            int dayAmount = Int32.Parse(row[RegistryJournal.DAY_AMOUNT_COLUMN_NAME].ToString());
            journal.DayAmount = dayAmount;

            int bail = Int32.Parse(row[RegistryJournal.BAIL_COLUMN_NAME].ToString());
            journal.Bail = bail;

            string issueDateString = row[RegistryJournal.ISSUE_DATE_COLUMN_NAME].ToString();
            journal.IssueDate = DateTime.Parse(issueDateString);

            string returnDateString = row[RegistryJournal.RETURN_DATE_COLUMN_NAME].ToString();
            if (returnDateString.Length > 0)
            {
                journal.ReturnDate = DateTime.Parse(returnDateString);
            }
        }
    }
}
